#!/bin/bash
#--20210813
#support NeoKylin-desktop-6.0,NeoKylin-server-6.5,rhel-6.5,Ubuntu

#回调os_ret=-1其他系统，1：麒麟桌面版64，2：麒麟服务器版，3：redhat6.5,4：ubuntu

function fw_start(){
  if [ "$(command -v systemctl)" ] && [ "$(command -v firewalld)" ];then
      systemctl start firewalld.service
			systemctl enable firewalld.service
	elif [ "$(command -v chkconfig)" ] && [ "$(command -v iptables)" ]; then
			service iptables start
			chkconfig iptables on
	elif [ "$(command -v ufw)" ]; then
			ufw enable
	else
	    echo "unknown this system"
	fi
}

function fw_stop(){
  if [ "$(command -v systemctl)" ] && [ "$(command -v firewalld)" ];then
      systemctl stop firewalld.service
			systemctl disable firewalld.service
	elif [ "$(command -v chkconfig)" ] && [ "$(command -v iptables)" ]; then
			service iptables stop
			chkconfig iptables off
	elif [ "$(command -v ufw)" ]; then
			ufw disable
	else
	    echo "unknown this system"
	fi
}
function fw_status(){
	if [ "$(command -v systemctl)" ] && [ "$(command -v firewalld)" ];  then
			cmd="`systemctl status firewalld.service|awk -F" " '/Active:/{print $3}'`"
			echo -e "\033[31m firewall is $cmd \033[0m"
	elif [ "$(command -v chkconfig)" ] && [ "$(command -v iptables)" ]; then
			service iptables status
	elif [ "$(command -v ufw)" ] ; then
			ufw status
	fi
}

case "$1" in
	start)
		fw_start
    echo "firewall start ok"
	;;
	stop)
		fw_stop
		echo "firewall close ok"
	;;
	status)
		fw_status
	;;
	*)
		echo "Please use start or stop or status as first argument"
	;;
esac
